package org.linlinjava.litemall.wx.web;

import org.linlinjava.litemall.core.util.ResponseUtil;
import org.linlinjava.litemall.db.domain.LitemallMoneyLog;
import org.linlinjava.litemall.db.domain.LitemallTiXian;
import org.linlinjava.litemall.db.domain.LitemallUser;
import org.linlinjava.litemall.db.domain.LitemallYHK;
import org.linlinjava.litemall.db.service.LitemallMoneyLogService;
import org.linlinjava.litemall.db.service.LitemallUserService;
import org.linlinjava.litemall.db.service.LitemallYHKService;
import org.linlinjava.litemall.wx.annotation.LoginUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotNull;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/wx/yhk")
@Validated
public class WxYHKController {

   @Autowired
    private LitemallYHKService litemallYHKService;
    @Autowired
    private LitemallUserService litemallUserService;

    @GetMapping("/list")
    public Object listwtx(@LoginUser Integer userId) {


        return ResponseUtil.okList(litemallYHKService.selectcarsbyid(userId));
    }

    @GetMapping("/detail")
    public Object findbyid( @NotNull Integer id) {


        return ResponseUtil.ok(litemallYHKService.carbyid(id));
    }
    @GetMapping("/mydef")
    public Object finddefbyid(@LoginUser Integer userId) {
        if(userId==null){return "空的登录信息，刷新重试";}
        LitemallYHK user =   litemallYHKService.isdefaultcar(userId);

        return ResponseUtil.ok(user==null?"暂无默认银行卡":user);
    }

    @PostMapping("/create")
    public Object create(@LoginUser Integer userId, @RequestBody LitemallYHK litemallYHK) {

        LitemallUser user = litemallUserService.findById(userId);

        litemallYHK.setUsername(user.getNickname());
        litemallYHK.setUserid(user.getId());
        return ResponseUtil.ok(litemallYHKService.insertyhk(litemallYHK));
    }

    @PostMapping("/update")
    public Object create(@RequestBody LitemallYHK litemallYHK) {


        return ResponseUtil.ok(litemallYHKService.updateyhk(litemallYHK));
    }

    @GetMapping("/delete")
    public Object deletes(@LoginUser Integer userId,@RequestParam(defaultValue = "0") int  id) {

LitemallYHK litemallYHK=new LitemallYHK();
litemallYHK.setId(id);
litemallYHK.setIsdelete(1);
        litemallYHK.setUserid(userId);
        return ResponseUtil.ok(litemallYHKService.updateyhk(litemallYHK));
    }

    @GetMapping("/setdef")
    public Object setdef(@LoginUser Integer userId,@RequestParam Integer  id) {

        litemallYHKService.setdef(userId);
        LitemallYHK litemallYHK=new LitemallYHK();
        litemallYHK.setId(id);
        litemallYHK.setIsdefault(1);
        litemallYHK.setUserid(userId);
        return ResponseUtil.ok(litemallYHKService.updateyhk(litemallYHK));
    }

}
